
local Tool=_G.CommonOHOH.load.WeaponTool();
local function_list =_G.ServerOHOH.function_list
local M=_G.ServerOHOH.class.new(script,  _G.ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);	
    self.DataStore=self.parent:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent())
    self.ItemCtrl=parent:GetComponent(_G.ServerOHOH.load.Server_ItemCtrl());
end;

function M.dtor(self)
	M.super.dtor(self);
end;

function M.Operate(self,opId,...)
    local fucId=opId
    assert(self[fucId],"opId==null:"..opId)
   local a = self[fucId](self,...)
--    print("+++++++++Operate+++++",a)
   return a
end;

--装备最佳
-- 返回值：true，成功，false 失败
function M.EquipBest(self) 
    local list={}
    for k,v in pairs(self.DataStore:GetData("weapon").had.__real) do
        if v>0 then
            table.insert(list,k)
        end
      
    end
   local idList= Tool.SortByPower(list)
--    print("------idList[1]---idList[1]-------------",idList[1])
   self.DataStore:GetData("weapon").equip=idList[1]

   self:UpdateBroWeapon()

   self.parent:SynchDataToClient("weapon")
   self.DataStore:Execut(function()
   end);

   return true
end


--装备参数:武器Id
-- 返回值：true，成功，false 失败
function M.Equip(self,weaponId)

    local equip=self.DataStore:GetData("weapon").equip
    if equip==weaponId then
        return true
    end
    local count=self.DataStore:GetData("weapon").had[weaponId]
    if  count and count>0 then
        self.DataStore:GetData("weapon").equip=weaponId

        self:UpdateBroWeapon()

        self.parent:SynchDataToClient("weapon")
        self.DataStore:Execut(function()
        end);
        return true
    else
        return false
    end
end

function M.BroEquip(self,weaponId,broId)
    local bro=self.DataStore:GetData("bro")
    local broInfo=bro.had.__real[broId]
    if not broInfo then --不存在好友
        return false
    end

    local BroEquip=self.DataStore:GetData("broEquip").__real
    if BroEquip[broId] and  BroEquip[broId]==weaponId then --本来就是装备的此物
        return true
    end

    local count=self.DataStore:GetData("weapon").had[weaponId]
    if not count or count<=0 then --不存在武器
        return false
    end


    self.DataStore:GetData("broEquip")[broId]=weaponId
    self:UpdateBroWeapon({broId})

    self.DataStore:Execut(function()
    end);

    --self.parent:SynchDataToClient("broEquip")
    -- print("--------------broEquip---------__real---------")
    -- _G.CommonOHOH:PrintTable(self.DataStore:GetData("broEquip").__real)

    return true
end


-- 升级:weapId
-- 返回值：true，成功，false 失败， 错误码：1：货币不够，2,其它未知原因
function M.Upgrade(self,weaponId)
    local errorCode,need= Tool.IsCanUpgrade(weaponId,self.DataStore)

    if  errorCode>0 then
        return false,errorCode
    end

    Tool.Upgrade(weaponId,self.DataStore)
    self.ItemCtrl:AddAnyThing("base_diamond",-need,false,false)

    self:UpdatPlayerBestEquip()

    self:UpdateBroWeapon()
    self.DataStore:Execut(function()
    end);

    self.parent:SynchDataToClient("weapon","base")

    return true
end

--如果玩家装备的被删除后，自动赋值最佳装备
function M.UpdatPlayerBestEquip(self)
    local equipId=self.DataStore:GetData("weapon").equip
    local had=self.DataStore:GetData("weapon").had.__real
 
    if not had[equipId] or had[equipId]<=0 then --玩家装备的被删除了
         local list={}
         for k,v in pairs(self.DataStore:GetData("weapon").had.__real) do
             if v>0 then
                 table.insert(list,k)
             end
         
         end
          local idList= Tool.SortByPower(list)
          if #idList==0 then
            self.DataStore:GetData("weapon").equip="1_1";
            self.DataStore:GetData("weapon").had.__real["1_1"]=1
          else
            self.DataStore:GetData("weapon").equip=idList[1]
          end
         
    end
 
end


function M.Remove(self,data)

    for id,count in pairs(data) do
       if  self.DataStore:GetData("weapon").had[id] then
            local oldCount=self.DataStore:GetData("weapon").had[id]
            local newCount=oldCount-count
            if newCount<=0 then
                self.DataStore:GetData("weapon").had[id]=nil
            else
                self.DataStore:GetData("weapon").had[id]=newCount
            end
       end
    end


    self:UpdatPlayerBestEquip()

    self:UpdateBroWeapon()

    self.parent:SynchDataToClient("weapon")
    self.DataStore:Execut(function()
    end);

    return true
end

--更新好友的武器，主要处理武器不够的情况下，自己和mustBroId，顶掉之前的
function M.UpdateBroWeapon(self,mustBroId)

    local BroEquipDataStore=self.DataStore:GetData("broEquip")
    mustBroId=mustBroId or {}
   local broEquipData=_G.CommonOHOH:DeepCopy(BroEquipDataStore.__real)
   local weaponData= _G.CommonOHOH:DeepCopy(self.DataStore:GetData("weapon").__real)
   local myequipId=weaponData.equip
   weaponData.had[myequipId]= weaponData.had[myequipId]-1
   local isUpdate=false
   local must={}
    for _,id in pairs(mustBroId) do
        local wid=  broEquipData[id]
        must[id]=true
        if  weaponData.had[wid] and  weaponData.had[wid]>0 then
            weaponData.had[wid]= weaponData.had[wid]-1
        else
            broEquipData[id]=nil
        end
        isUpdate=true
   end

   for broId,wid in pairs(broEquipData) do
        if not must[broId] then
            if  weaponData.had[wid] and  weaponData.had[wid]>0 then
                weaponData.had[wid]= weaponData.had[wid]-1
            else
                broEquipData[broId]=nil
                isUpdate=true
            end
        end
   end
   if isUpdate then
    --    BroEquipDataStore=broEquipData
       self.DataStore:SetData("broEquip",broEquipData)
       
       print("------UpdateBroWeapon------------")
       _G.CommonOHOH:PrintTable(broEquipData)
       _G.CommonOHOH:PrintTable(self.DataStore:GetData("broEquip").__real)

        self.parent:SynchDataToClient("broEquip")
   end
   return isUpdate
end


--获取弓箭模型信息
function M.GetWeaponArt(self,weaponId)
    return Tool.GetWeaponArt(weaponId)
end


function M.WeaponAttack(self,...)
    if self.WeaponObj then
        self.WeaponObj:Attack(...)
    end
end


M:RegistFunction(function_list.weapon_Operate,M.Operate)


return M